#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101

#include <Macros.h>
#include <CoVector.h>
#include <CoMatrix.h>
#include <CoCube.h>
#include <ValueHelpers.h>
#include <Settings.h>

#pragma unmanaged 
#include <ql\experimental\credit\defaulttype.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;

#undef HANDLE
#undef ABSTRACT
#undef STRUCT
namespace Cephei { namespace QL { namespace Experimental { namespace Credit {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IDefaultType
	public ref class CDefaultType : 
            public System::MarshalByRefObject,
            public Cephei::QL::Experimental::Credit::IDefaultType
	{
	protected: 
		boost::shared_ptr<QuantLib::DefaultType>* _ppDefaultType;
        boost::detail::spinlock* _pSpinlock;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::DefaultType>* _phDefaultType;
#endif
		Object^ _DefaultTypeOwner;     // reference to object that manages the storage for this object
	internal:
		CDefaultType (Microsoft::FSharp::Core::FSharpOption<QL::Experimental::Credit::AtomicDefault::TypeEnum>^ defType, Microsoft::FSharp::Core::FSharpOption<QL::Experimental::Credit::Restructuring::TypeEnum>^ restType);
        CDefaultType (boost::shared_ptr<QuantLib::DefaultType>& childNative, Object^ owner);
        CDefaultType (QuantLib::DefaultType& childNative, Object^ owner);
        CDefaultType (CDefaultType^ copy);
        CDefaultType (System::Type^ t);
#ifdef STRUCT
        CDefaultType (QuantLib::DefaultType childNative);
#endif       
#ifdef HANDLE
		CDefaultType (QuantLib::Handle<QuantLib::DefaultType>& childNative, Object^ owner);
		CDefaultType (QuantLib::Handle<QuantLib::DefaultType> childNative);
#endif
		virtual ~CDefaultType ();
		!CDefaultType ();

	internal:
		QuantLib::DefaultType& GetReference ();
		boost::shared_ptr<QuantLib::DefaultType>& GetShared ();
		QuantLib::DefaultType* GetPointer ();
        void SetDefaultType (boost::shared_ptr<QuantLib::DefaultType> native)
        {
            if (_ppDefaultType != NULL)
                delete _ppDefaultType;
            _ppDefaultType = new boost::shared_ptr<QuantLib::DefaultType> (native);
            
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::DefaultType>& GetHandle ();
#endif
		virtual bool HasNative () ;
		void Lock ()
		{
			if (Cephei::QL::Settings::_ReferenceLocking)
    			_pSpinlock->lock ();
		}
		void Unlock ()
		{
			_pSpinlock->unlock ();
		}
    public:
		virtual Boolean ContainsDefaultType (QL::Experimental::Credit::AtomicDefault::TypeEnum defType) ;
		virtual Boolean ContainsRestructuringType (QL::Experimental::Credit::Restructuring::TypeEnum resType) ;
        property QL::Experimental::Credit::AtomicDefault::TypeEnum DefaultType 
        {
		    virtual QL::Experimental::Credit::AtomicDefault::TypeEnum get () ;
        }
        property Boolean IsRestructuring 
        {
		    virtual Boolean get () ;
        }
        property QL::Experimental::Credit::Restructuring::TypeEnum RestructuringType 
        {
		    virtual QL::Experimental::Credit::Restructuring::TypeEnum get () ;
        }
    };
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IDefaultType Vector interface
	ref class CDefaultTypeVector : public System::MarshalByRefObject,  
							    public INativeVector<IDefaultType^>,
							    public INativeVector<Generic::ICell<IDefaultType^>^>
	{
	private:
#ifdef STRUCT
		std::vector<QuantLib::DefaultType>* _pVValue;
#endif        
		std::vector<boost::shared_ptr<QuantLib::DefaultType>>* _pVBoost;
#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::DefaultType>>* _pVHand;
#endif
		Object^ _owner;		// object reference to prevent GC of vector owner

	internal:
		CDefaultTypeVector ();
#ifdef STRUCT
		CDefaultTypeVector (std::vector<QuantLib::DefaultType>& vec, Object^ owner);
		CDefaultTypeVector (std::vector<QuantLib::DefaultType> vec);
#endif       
		CDefaultTypeVector (std::vector<boost::shared_ptr<QuantLib::DefaultType>>& vec, Object^ owner);
#ifdef HANDLE
		CDefaultTypeVector (std::vector<QuantLib::Handle<QuantLib::DefaultType>>& vec, Object^ owner);
#endif
		virtual ~CDefaultTypeVector ();
		!CDefaultTypeVector ();

    public:
		property int Count 
		{
			virtual int get ();
		}

		property IDefaultType^ default[int] 
		{
			virtual IDefaultType^ get (int index) = INativeVector<IDefaultType^>::default::get;
			virtual void set (int index, IDefaultType^ value) = INativeVector<IDefaultType^>::default::set;
		}

		//////////////////////////////////////////////////////////////////////////////////////////
		// Interface functions for cell vectors 
	public:
		property Generic::ICell<Cephei::QL::Experimental::Credit::IDefaultType^>^ CItem[int] 
		{
			virtual Generic::ICell<IDefaultType^>^ get (int index) = INativeVector<Generic::ICell<IDefaultType^>^>::default::get;
			virtual void set (int index, Generic::ICell<IDefaultType^>^ value) = INativeVector<Generic::ICell<IDefaultType^>^>::default::set;
		}
	internal:
#ifdef STRUCT    
		std::vector<QuantLib::DefaultType>& GetReference ();
#endif     
		std::vector<boost::shared_ptr<QuantLib::DefaultType>>& GetShared ();

#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::DefaultType>>& GetHandle ();
#endif
	public:
		virtual bool PrepareFeature (NativeFeature feature);
	};
    
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
	public ref class CDefaultType_Factory : public System::MarshalByRefObject,  public IDefaultType_Factory
	{
	public:
        virtual IDefaultType^ Create (Microsoft::FSharp::Core::FSharpOption<QL::Experimental::Credit::AtomicDefault::TypeEnum>^ defType, Microsoft::FSharp::Core::FSharpOption<QL::Experimental::Credit::Restructuring::TypeEnum>^ restType);
        /* <summary> Create a hybrid Vector of IDefaultType, with event notification of changes </summary> */
        virtual Cephei::IVector<IDefaultType^>^ CreateVector();
        /* <summary> Create a hybrid Vector of ICell of IDefaultType, with event notification of changes </summary> */
        virtual Generic::ICell<Cephei::IVector<Generic::ICell<IDefaultType^>^>^>^ CreateCellVector();
        virtual Cephei::IVector<IDefaultType^>^ CreateVector(System::Collections::Generic::IEnumerable<IDefaultType^>^ source);
        virtual Generic::ICell<Cephei::IVector<Generic::ICell<IDefaultType^>^>^>^ CreateCellVector(System::Collections::Generic::IEnumerable<Generic::ICell<IDefaultType^>^>^ source);
    };
   
/*Cephei*/ } /*QL*/ } /*Experimental*/ } /*Credit */}
